import pymouse
import pykeyboard

myPyMouse = pymouse.PyMouse()
myPyKeyboard = pykeyboard.PyKeyboard()


def click(x, y):
    """鼠标单击一个坐标，完后自动移动回去"""
    pos = myPyMouse.position()
    myPyMouse.click(x, y)
    myPyMouse.move(pos[0], pos[1])


from PIL import ImageGrab


def matchImg(imgsrc, imgobj, confidencevalue=0.9):
    """找图  参数（原图，要找的图，确信度
    需要先安装 pip install Pillow"""
    imsrc = ac.imread(imgsrc)
    imobj = ac.imread(imgobj)

    match_result = ac.find_template(imsrc, imobj, confidencevalue)
    if match_result is not None:
        match_result['shape'] = (imsrc.shape[1], imsrc.shape[0])

    return match_result


import aircv as ac


def saveScreenshot(savePath):
    """保存屏幕截图"""
    img = ImageGrab.grab()
    img.save(savePath, 'jpeg')  # 保存位置，文件格式


def findPic(img2findPath):
    """找图,if result is not None就是找到了"""
    saveScreenshot(r'images\src.jpg')
    result = matchImg(r'images\src.jpg', img2findPath)
    return result


import psutil


def lolClientExist():
    """lol客户端进程是否正在运行"""
    for proc in psutil.process_iter():
        pinfo = proc.as_dict(attrs=['name'])
        if pinfo['name'] == "LeagueClient.exe":
            return True
            break
    return False


def lolGameExist():
    """lol正式游戏进程是否正在运行"""
    for proc in psutil.process_iter():
        pinfo = proc.as_dict(attrs=['name'])
        if pinfo['name'] == "League of Legends.exe":
            return True
            break
    return False


from data import ClientState

# 一张图确定客户端在什么状态
stateImageStrs = "play-btn.jpg", "queueing-btn.jpg", "advanced-data.jpg"


def getCurrClientState():
    """获得当前lol客户端在什么状态下，显示的什么画面"""
    if not lolClientExist():
        print("当前状态：LOL未运行")
    elif not lolGameExist():

        for i in range(stateImageStrs.__len__()):
            result = findPic("images/" + stateImageStrs[i])
            if result is not None:  # 找到图了就跳出
                break

        if result is None:
            i = -1
        if i == -1:
            print("当前状态：什么都没找到")
            return ClientState.NoneState
        elif i == 0:
            print("当前状态：大厅")
            return ClientState.Lobby
        elif i == 1:
            print("当前状态：队列中")
            return ClientState.OnQueue
        elif i == 2:
            print("当前状态：结束页面")
            return ClientState.OnOverList
